<template>
  <div>
    <!-- 标题-->
    <van-nav-bar title="登录" />
    <!-- 登录表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="info.mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="rules.mobile"
      />
      <van-field
        v-model="info.code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="rules.code"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :loading="isLoding"
          loading-text="登录中"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
// 导入
export default {
  data() {
    return {
      isLoding: false,
      info: {
        mobile: "",
        code: "",
      },
      rules: {
        mobile: [
          { required: true, message: "请填写手机号" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号" },
        ],
        code: [
          { required: true, message: "请填写验证码" },
          { pattern: /^\d{6}$/, message: "请输入6位数字验证码" },
        ],
      },
    };
  },
  methods: {
    async onSubmit() {
      // 将控制登录中的布尔值设置为true;
      this.isLoding = true;
      const result = await this.$store.dispatch("login", this.info);
      //
      if (result) {
        // 退出功能bug解决
        this.$store.commit("setUserInfo", {});
        if (this.$route.query.back) {
          // console.log(this.$route.query.back);
          this.$router.push(this.$route.query.back);
        } else {
          this.$router.push("/layout/home");
        }
      }
      // 将控制登录中的布尔值设置为false;
      this.isLoding = false;
    },
  },
};
</script>

<style>
</style>